package com.labbol.core.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 多数据源dao,测试

 * @author fuxl
 * @since 2018年2月13日下午4:56:33
 * @version v1.0
 */
public class DynamicDBDao {

	private JdbcTemplate jdbcTemplate;

	public DynamicDBDao() {}

	public DynamicDBDao(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/**
	 * 获取jdbcTemplate
	 * @return
	 */
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void initJdbcTemplate(BasicDataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public List<Map<String, Object>> queryList(String sql, Object... param) {
		List<Map<String, Object>> list;
		if (ArrayUtils.isEmpty(param)) {
			list = jdbcTemplate.queryForList(sql);
		} else {
			list = jdbcTemplate.queryForList(sql, param);
		}
		return list;
	}

	public <T> List<T> queryList(String sql, Class<T> clazz, Object... param) {
		List<T> list;

		if (ArrayUtils.isEmpty(param)) {
			list = jdbcTemplate.queryForList(sql, clazz);
		} else {
			list = jdbcTemplate.queryForList(sql, clazz, param);
		}
		return list;
	}
	
}
